package com.dodock.banglapapers.server.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

public class ProthomAloDetailsGenerator {
	
	String id;
	
	public ProthomAloDetailsGenerator(String id){
		this.id = id;
	}
	
/*
	public String generate() throws IOException{
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();		
		
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey("NewsDetails", id));
			Text text = (Text)detailsEntity.getProperty("data");
			return text.getValue();
			
		} catch (EntityNotFoundException e) {
			Document doc = Jsoup.connect("http://m.prothom-alo.com/details.php?newsId="+id).get();
			Elements divs = doc.select("div[style=color:#333]");
			
			if(divs != null && divs.size()>0){
				String data = String.format("<news>%s</news>", divs.get(0).html());
				Text text = new Text(data);
				Entity detailsEndity = new Entity("NewsDetails", id);
				detailsEndity.setProperty("data", text);
				datastore.put(detailsEndity);
				return data;
			}	
		}
		
		return "";
	}
*/	
	
	/*
	public String generate() throws IOException{
		
		Document doc = Jsoup.connect("http://m.prothom-alo.com/details.php?newsId="+id).get();
		Elements divs = doc.select("div[style=color:#333]");
		
		if(divs != null && divs.size()>0){
			return String.format("<news>%s</news>", divs.get(0).text());
		}
		
		return "";
	}
	*/
	
	public String generate2() throws IOException{
			// Get a handle on the datastore itself
				DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();		
				// Lookup data by known key name
				try {
					Entity detailsEntity = datastore.get(KeyFactory.createKey("NewsDetails", id));
					Text text = (Text)detailsEntity.getProperty("data");
					return text.getValue();
					
				} catch (EntityNotFoundException e) {
					
					URL url = new URL(String.format(Settings.WWW_PROTHOM_ALO_COM + "/mobileapp/FetchById.php?newsid=%s&image=on&quaity=high", id));
					BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
					String line;
					StringBuilder response = new StringBuilder("");
					while((line=reader.readLine()) != null){
						response.append(line);
					}
					
					
					String data = response.toString();
					Text text = new Text(data);
					/* save here */
					Entity detailsEndity = new Entity("NewsDetails", id);
					detailsEndity.setProperty("data", text);
					datastore.put(detailsEndity);
					return data;
						
				}
	}
	
}
